package com.kzd.gtgame.main.mine

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.entity.Feedback
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.home.adapter.ImageSelectAdapter
import com.module.lemlin.http.Status

import com.module.lemlin.owner.OwnerViewModeFragment
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.fragment_feedback_comp.*
import java.lang.StringBuilder


class FeedbackCompFragment : OwnerViewModeFragment<MineViewModel>() {

    companion object {
        fun newInstance(): FeedbackCompFragment {
            return FeedbackCompFragment()
        }
    }

    private val mFeedback: Feedback = Feedback(type = "1")

    private var mImages: MutableList<String> = mutableListOf()
    private val mImageSelectAdapter = ImageSelectAdapter()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_feedback_comp

    override fun initView() {
        rvFeedbackCompImageBody.adapter = mImageSelectAdapter.apply {
            setNewInstance(mutableListOf(R.mipmap.photo_add_icon))
        }
        mImageSelectAdapter.setOnItemClickListener { _, _, position ->
            if (position != 0) {
                return@setOnItemClickListener
            }
            viewModel.userMatisse(fragment = this, maxSelectable = 7)
        }

        tvFeedbackCompObject.addTextChangedListener {
            mFeedback.apply { complaint = it?.toString() }
        }
        tvFeedbackCompPhone.addTextChangedListener {
            mFeedback.apply { complaint_way = it?.toString() }
        }
        tvFeedbackCompContent.addTextChangedListener {
            mFeedback.apply { content = it?.toString() }
        }
        tvFeedbackCompWx.addTextChangedListener {
            mFeedback.apply { qq = it?.toString() }
        }

        tvFeedbackCompSubmit.setOnClickAntiShakeListener {
            when {
                tvFeedbackCompObject.text.isNullOrEmpty() -> {
                    toast("请输入投诉对象")
                    return@setOnClickAntiShakeListener
                }
                tvFeedbackCompPhone.text.isNullOrEmpty() -> {
                    toast("请输入联系方式")
                    return@setOnClickAntiShakeListener
                }
                tvFeedbackCompContent.text.isNullOrEmpty() -> {
                    toast("请输入投诉内容")
                    return@setOnClickAntiShakeListener
                }
                tvFeedbackCompWx.text.isNullOrEmpty() -> {
                    toast("请输入联系QQ")
                    return@setOnClickAntiShakeListener
                }
                mImages.isNullOrEmpty() -> {
                    toast("请选择图片")
                    return@setOnClickAntiShakeListener
                }
                else -> viewModel.userUpload(images = mImages)
            }
        }
    }

    override fun initData() {

    }

    override fun initViewMode() {

        viewModel.mineFeedbackLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            toast(response.data?.message)
            requireActivity().finish()
        })

        viewModel.userUploadLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.size == 0)
                return@observe
            val images = StringBuilder()
            data.forEach {
                images.append(it).append(",")
            }
            if (images.isNotEmpty()) {
                mFeedback.apply {
                    this.imgs = images.substring(0, images.length - 1).toString()
                }
            }
            viewModel.mineFeedback(mFeedback)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == AppCompatActivity.RESULT_OK) {
            val pathList: MutableList<String> = Matisse.obtainPathResult(data).toMutableList()
            if (pathList.size == 0)
                return
            val images: MutableList<Any> = mutableListOf()
            images.add(R.mipmap.photo_add_icon)
            pathList.forEach {
                images.add(it)
            }
            mImageSelectAdapter.setNewInstance(images)
            mImages.clear()
            mImages.addAll(pathList)
        }
    }

}